package com.bitdf.txing.group.feign.impl;

import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.group.feign.EsFeignService;
import com.bitdf.txing.group.to.GroupInfoTo;
import com.bitdf.txing.search.service.GroupSaveService;
import com.bitdf.txing.search.service.GroupUpdateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author Lizhiwei
 * @date 2023/9/5 23:02:28
 * 注释：
 */
@Service
@Slf4j
@ConditionalOnProperty(value = "tx.deploy.mode", havingValue = "standalone")
public class EsFeignServiceImpl implements EsFeignService {

    @Autowired
    GroupSaveService groupSaveService;
    @Autowired
    GroupUpdateService groupUpdateService;
    @Override
    public R productStatusUp(GroupInfoTo groupInfoTo) throws IOException {
        com.bitdf.txing.search.to.GroupInfoTo infoTo = new com.bitdf.txing.search.to.GroupInfoTo();
        BeanUtils.copyProperties(groupInfoTo, infoTo);
        boolean b=groupSaveService.saveGroup(infoTo);
        log.info("组团id:{}发布是否成功{}", groupInfoTo.getId(), b);
        return b ? R.ok() : R.error();
    }

//    @Override
//    public R modifyLikeCount(Long groupId, Long count) throws IOException {
//        return null;
//    }
//
//    @Override
//    public R modifyViewCount(Long groupId) throws IOException {
//        return null;
//    }

    @Override
    public R closeGroup(Long groupId) throws IOException {
        Boolean b = groupUpdateService.modifyStatus(groupId);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION);
    }

    @Override
    public R countIncreOrDcre(Long groupId, Integer opsValue, String target) {
        Boolean b = null;
        try {
            b = groupUpdateService.increOrDcre(groupId, opsValue, target);
        } catch (IOException e) {
            log.info("{}", e);
        }
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getCode(), TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getMsg());
    }

    @Override
    public R updateGroup(GroupInfoTo groupInfoTo) throws IOException {
        com.bitdf.txing.search.to.GroupInfoTo groupInfoTo1 = new com.bitdf.txing.search.to.GroupInfoTo();
        BeanUtils.copyProperties(groupInfoTo, groupInfoTo1);
        // 发生了修改 修改标志位设置为 1
        groupInfoTo1.setModifyFlag(1);
        boolean b = groupUpdateService.updateGroup(groupInfoTo1);
        log.info("组团id： {} 修改成功： {}", groupInfoTo.getId(), b);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getCode(), TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getMsg());
    }
}
